<template>
    <div>
        <front-header />
        <div class="list-container">
            <div class="list-title">专家顾问</div>
            <div class="search-row">
                <el-input v-model="search" placeholder="搜索专家姓名/领域" prefix-icon="el-icon-search" clearable />
            </div>
            <el-row :gutter="24" class="expert-row">
                <el-col :span="8" v-for="expert in filteredExperts" :key="expert.id">
                    <el-card @click.native="goDetail(expert.userId)" class="expert-card">
                        <img :src="getImageUrl(expert.avatar)" alt="头像" class="avatar" />
                        <div class="info">
                            <div class="name">{{ expert.name }}</div>
                            <div class="title">{{ expert.title }}</div>
                            <div class="field">{{ expert.field }}</div>
                        </div>
                    </el-card>
                </el-col>
            </el-row>
            <el-empty v-if="!loading && filteredExperts.length === 0" description="暂无专家" />
        </div>
        <front-footer />
    </div>
</template>
<script>
import { getExpertList } from '../../api/ExpertController';
import FrontHeader from "@/components/front/FrontHeader.vue";
import FrontFooter from "@/components/front/FrontFooter.vue";
export default {
    components: { FrontFooter, FrontHeader },
    data() {
        return {
            experts: [],
            loading: true,
            search: ''
        };
    },
    computed: {
        filteredExperts() {
            if (!this.search) return this.experts;
            return this.experts.filter(e =>
                (e.name && e.name.includes(this.search)) ||
                (e.field && e.field.includes(this.search))
            );
        }
    },
    methods: {
        goDetail(id) {
            this.$router.push({ name: 'ExpertDetail', params: { expertId: id } });
        },
        getImageUrl(url) {
            if (!url) return ''
            return url.startsWith('/api') ? url : `/api${url}`
        },
        async loadExperts() {
            this.loading = true;
            const resp = await getExpertList();
            const data = [];
            for (const expert of resp) {
                data.push({
                    id: expert.expert.id,
                    name: expert.expert.name,
                    title: expert.expert.title,
                    field: expert.expert.field,
                    avatar: expert.expert.avatar,
                    userId: expert.expert.userId
                });
            }
            this.experts = data;
            this.loading = false;
        }
    },
    mounted() {
        this.loadExperts();
    }
};
</script>
<style scoped>
.list-container {
    padding: 48px 32px;
    min-height: 80vh;
    background: #f9f9f9;
    padding-bottom: 40px;
    max-height: 100%;
    overflow: auto;
}

.list-title {
    font-size: 32px;
    font-weight: 700;
    text-align: center;
    margin: 32px 0 28px 0;
    color: #333333;
    position: relative;
    display: inline-block;
    left: 50%;
    transform: translateX(-50%);
}

.list-title::after {
    content: '';
    position: absolute;
    bottom: -8px;
    left: 0;
    width: 100%;
    height: 4px;
    background: linear-gradient(90deg, #4caf50 0%, #81c784 100%);
    border-radius: 2px;
}

.search-row {
    display: flex;
    justify-content: center;
    margin-bottom: 40px;
}

.search-row .el-input {
    width: 400px;
}

.search-row .el-input :deep(.el-input__inner) {
    background: #ffffff;
    border: 1px solid #dcdfe6;
    color: #333333;
    border-radius: 24px;
    height: 44px;
    font-size: 15px;
}

.search-row .el-input :deep(.el-input__inner:focus) {
    border-color: #4caf50;
}

.search-row .el-input :deep(.el-input__prefix) {
    left: 15px;
}

.search-row .el-input :deep(.el-input__icon) {
    color: #4caf50;
    font-size: 18px;
}

.expert-row {
    max-width: 1200px;
    margin: 0 auto;
}

.expert-card {
    cursor: pointer;
    transition: all 0.3s ease;
    margin-bottom: 32px;
    border-radius: 12px;
    background: #ffffff;
    border: 1px solid #eaeaea;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
    padding: 24px 0 18px 0;
}

.expert-card:hover {
    box-shadow: 0 12px 28px rgba(76, 175, 80, 0.2);
    transform: translateY(-8px);
    border-color: #4caf50;
}

.avatar {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    object-fit: cover;
    display: block;
    margin: 0 auto;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
    border: 2px solid #eaeaea;
    transition: all 0.3s ease;
}

.expert-card:hover .avatar {
    border-color: #4caf50;
    transform: scale(1.05);
}

.info {
    margin-top: 16px;
    text-align: center;
    padding: 0 16px;
}

.name {
    font-weight: 600;
    font-size: 18px;
    color: #333333;
    margin-bottom: 8px;
}

.title {
    color: #4caf50;
    margin-top: 4px;
    font-size: 15px;
}

.field {
    color: #666666;
    margin-top: 4px;
    font-size: 14px;
}

@media (max-width: 1200px) {
    .expert-row .el-col {
        width: 50%;
    }
}

@media (max-width: 768px) {
    .expert-row .el-col {
        width: 100%;
    }
    .list-container {
        padding: 32px 16px;
    }
    .list-title {
        font-size: 26px;
    }
    .search-row .el-input {
        width: 100%;
    }
}
</style>